import java.util.Scanner;

public class main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        in.nextLine();
        String[] s = in.nextLine().split(" ");
        String str1 = s[0],str2 = s[1];

        String[] strs = new String[n];

        for(int i = 0;i < n;i++) {
            String tmp = in.nextLine();
            strs[i] = tmp;
        }

        int prev1 = -1;
        int prev2 = -1;
        int minDist = n;

        for(int i = 0;i < n;i++) {
            if(strs[i].equals(str1)) {
                if(prev2 != -1) {
                    minDist = Math.min(minDist,i - prev2);
                }
                prev1 = i;
            }
            if(strs[i].equals(str2)) {
                if(prev1 != -1) {
                    minDist = Math.min(minDist,i - prev1);
                }
                prev2 = i;
            }
        }

        if(minDist == n) {
            System.out.println(-1);
        } else {
            System.out.println(minDist);
        }
    }
}